/*
 * @Author: Mcc
 * @Date: 2024-07-09 11:11:54
 * @LastEditors: mcc 550539422@qq.com
 * @LastEditTime: 2024-08-20 16:13:34
 * @FilePath: \ft--pc-apply\src\routers\index.ts
 * @Description:
 *
 * Copyright (c) 2024 by Mcc, All Rights Reserved.
 */

import { createRouter, createWebHashHistory } from "vue-router";

import { useStore } from "../store";
import NProgress from "nprogress";
import { message } from "ant-design-vue";
//引入pinia后要在beforeEach中定义
const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: "/login",
      name: "login",
      meta: { title: "登录", index: "3" },
      component: () => import("@/views/login.vue"),
    },
    {
      path: "/index",
      name: "index",
      meta: { title: "首页", index: "1" },
      component: () => import("@/views/index/Index.vue"),
    },
    {
      path: "/",
      name: "join",
      meta: { title: "简历填写", index: "2", bgwhite: true },
      component: () => import("@/views/join/Join.vue"),
    },
    {
      path: "/privacyPolicytPrivew",
      name: "privacyPolicytPrivew",
      meta: {

        title: "隐私政策",
        index: "2",
        bgwhite: true,
      },
      component: () => import("@/views/join/PrivacyPolicytPrivew.vue"),
    },
    {
      path: "/trainAgreement",
      name: "trainAgreement",
      meta: {

        title: "方田教育新田计划特训营培训协议",
        index: "2",
        bgwhite: true,
      },
      component: () => import("@/views/train/TrainAgreement.vue"),
    },
    {
      path: "/trainAgreementPrivew",
      name: "trainAgreementPrivew",
      meta: {
        title: "方田教育新田计划特训营培训协议预览",
        index: "3",
        bgwhite: true,
      },
      component: () => import("@/views/train/TrainAgreementPrivew.vue"),
    },
    {
      path: "/progress",
      name: "progress",
      meta: { title: "应聘进度", index: "2", bgwhite: true },
      component: () => import("@/views/progress/ApplicationProgress.vue"),
    },
    {
      path: "/contract",
      name: "contract",
      meta: { title: "应约协议", index: "7", bgwhite: true },
      component: () => import("@/views/progress/ContractAgreement.vue"),
    },
    {
      path: "/ContractAgreementPrivew",
      name: "ContractAgreementPrivew",
      meta: { title: "应约协议预览", index: "8", bgwhite: true },
      component: () => import("@/views/progress/ContractAgreementPrivew.vue"),
    },
  ],
});
// 设置页面标题
router.beforeEach(async (to, _from, next) => {
  const store = useStore();
  //console.log(to.path);
  // if (!store && to.path !== "/") {
  //   message.error("未查询到登录信息，请先登录");
  //   next("/login");
  //   return;
  // }

  NProgress.configure({ showSpinner: false });
  NProgress.start();
  const { hasLogin } = store;
  if (!hasLogin && to.path !== "/login") {
    message.error("未登录，请先登录");
    next("/login");
    return;
  }
  store.routerIndex = to.meta.index as string;
  document.title = String(to.meta.title) || "方田网申系统"; // 可以在路由的 meta 中设置标题
  next();
  //可以继续判定是否存在token，如果存在则跳转首页，否则跳转登录页面
});
router.afterEach(() => {
  NProgress.done();
});

// 导出
export default router;
